/*
 * $Id: QSO.m 100 2009-08-13 01:09:06Z jon@bway.net $
 * Chalk
 *
 * Created 7/6/09
 * Copyright Jon Gordon
 * 
 * Permission is hereby granted, free of charge, to any person obtaining 
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including 
 * without limitation the rights to use, copy, modify, merge, publish, 
 * distribute, sublicense, and/or sell copies of the Software, and to 
 * permit persons to whom the Software is furnished to do so, subject to 
 * the following conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

#import "QSO.h"

#import "QECallsign.h"
#import "QEHamData.h"

static NSCalendar *gregorianCalendar = nil;

static const unsigned dateFlags = NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit;
static const unsigned timeFlags = NSHourCalendarUnit | NSMinuteCalendarUnit |  NSSecondCalendarUnit;
static const unsigned allFlags = NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit |  NSSecondCalendarUnit;


@implementation QSO 

@dynamic band;
@dynamic callsign;
@dynamic comment;
@dynamic freq;
@dynamic isXFreq;
@dynamic isXMode;
@dynamic mode;
@dynamic preservedFields;
@dynamic qsl_lotw_rcvd;
@dynamic qsl_lotw_sent;
@dynamic qsl_rcvd;
@dynamic qsl_sent;
@dynamic qso_date;
@dynamic qso_end;
@dynamic receive_freq;
@dynamic receive_mode;
@dynamic rst_rcvd;
@dynamic rst_sent;
@dynamic wattsOut;


+ (void)initialize
{
    NSLog(@"MESSAGE: %s", __PRETTY_FUNCTION__);
    static BOOL initialized = NO;
    
    if (!initialized) {
        NSLog(@"  initializing");
        initialized = YES;
        gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
        [gregorianCalendar setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];
    } // if
} // initialize


- (void)setFreq:(NSDecimalNumber *)newValue
{
    NSLog(@"MESSAGE: %s", __PRETTY_FUNCTION__);
    if (self.freq != newValue) {
        NSLog(@"  updating value");
        [self willChangeValueForKey:@"freq"];
        [self setPrimitiveValue:newValue forKey:@"freq"];
        [self didChangeValueForKey:@"freq"];
        
        if ((nil == self.band) || (0 == [self.band length])) {
            QEBand *newBand = [QEBand bandContainingFrequency:newValue];
            if (nil != newBand) {
                self.band = [newBand name];
            } // if
        } //
    } // if
} // setFreq:


- (BOOL)validateBand:(id *)ioValue error:(NSError **)outError
{
    NSLog(@"MESSAGE: %s", __PRETTY_FUNCTION__);
    return [QEBand validateBand:ioValue error:outError];
} // validateBand:error:


- (BOOL)validateCallsign:(id *)ioValue error:(NSError **)outError
{
    NSLog(@"MESSAGE: %s", __PRETTY_FUNCTION__);
    return [QECallsign isValidCallsign:ioValue error:outError];
} // validateCallsign:error:


- (BOOL)validateMode:(id *)ioValue error:(NSError **)outError
{
    NSLog(@"MESSAGE: %s", __PRETTY_FUNCTION__);
    return [QEMode validateMode:ioValue error:outError];
} // validateMode:error:


- (BOOL)validateReceive_mode:(id *)ioValue error:(NSError **)outError
{
    NSLog(@"MESSAGE: %s", __PRETTY_FUNCTION__);
    return [QEMode validateMode:ioValue error:outError];
} // validateReceive_mode:error:


+ (NSSet *)keyPathsForValuesAffectingDatePartOfDate
{
    return [NSSet setWithObject:@"qso_date"];
} // keyPathsForValuesAffectingDatePartOfDate

- (void)setDatePartOfDate:(NSDate *)newDate
{
    NSDateComponents *comps = [gregorianCalendar components:allFlags
                                                   fromDate:self.qso_date];
    NSDateComponents *newComps = [gregorianCalendar components:dateFlags
                                                      fromDate:newDate];
    [comps setYear:[newComps year]];
    [comps setMonth:[newComps month]];
    [comps setDay:[newComps day]];
    
    self.qso_date = [gregorianCalendar dateFromComponents:comps];
} // setDatePartOfDate:

- (NSDate *)datePartOfDate
{
    return self.qso_date;
} // datePartOfDate


+ (NSSet *)keyPathsForValuesAffectingTimePartOfDate
{
    return [NSSet setWithObject:@"qso_date"];
} // keyPathsForValuesAffectingTimePartOfDate

- (void)setTimePartOfDate:(NSDate *)newTime 
{
    NSDateComponents *comps = [gregorianCalendar components:allFlags
                                                   fromDate:self.qso_date];
    NSDateComponents *newComps = [gregorianCalendar components:timeFlags
                                                      fromDate:newTime];
    [comps setHour:[newComps hour]];
    [comps setMinute:[newComps minute]];
    [comps setSecond:[newComps second]];
    
    self.qso_date = [gregorianCalendar dateFromComponents:comps];
} // setTimePartOfDate

- (NSDate *)timePartOfDate
{
    return self.qso_date;
} // timePartOfDate
@end
